<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="generator" content="hevea 2.18">
<link rel="stylesheet" type="text/css" href="manual.css">
<title>Chapter&#XA0;9&#XA0;&#XA0;Batch compilation (ocamlc)</title>
</head>
<body>
<a href="extn.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="toplevel.html"><img src="next_motif.gif" alt="Next"></a>
<hr>
<h1 class="chapter" id="sec279">Chapter&#XA0;9&#XA0;&#XA0;Batch compilation (ocamlc)</h1>
<ul>
<li><a href="comp.html#sec280">9.1&#XA0;&#XA0;Overview of the compiler</a>
</li><li><a href="comp.html#sec281">9.2&#XA0;&#XA0;Options</a>
</li><li><a href="comp.html#sec283">9.3&#XA0;&#XA0;Modules and the file system</a>
</li><li><a href="comp.html#sec284">9.4&#XA0;&#XA0;Common errors</a>
</li><li><a href="comp.html#sec285">9.5&#XA0;&#XA0;Warning reference</a>
</li></ul>
<p> <a id="c:camlc"></a>

</p><p>This chapter describes the OCaml batch compiler <span class="c003">ocamlc</span>,
which compiles OCaml source files to bytecode object files and links
these object files to produce standalone bytecode executable files.
These executable files are then run by the bytecode interpreter
<span class="c003">ocamlrun</span>.</p>
<h2 class="section" id="sec280">9.1&#XA0;&#XA0;Overview of the compiler</h2>
<p>The <span class="c003">ocamlc</span> command has a command-line interface similar to the one of
most C compilers. It accepts several types of arguments and processes them
sequentially, after all options have been processed:</p><ul class="itemize"><li class="li-itemize">
Arguments ending in <span class="c003">.mli</span> are taken to be source files for
compilation unit interfaces. Interfaces specify the names exported by
compilation units: they declare value names with their types, define
public data types, declare abstract data types, and so on. From the
file <span class="c009">x</span><span class="c003">.mli</span>, the <span class="c003">ocamlc</span> compiler produces a compiled interface
in the file <span class="c009">x</span><span class="c003">.cmi</span>.</li><li class="li-itemize">Arguments ending in <span class="c003">.ml</span> are taken to be source files for compilation
unit implementations. Implementations provide definitions for the
names exported by the unit, and also contain expressions to be
evaluated for their side-effects. From the file <span class="c009">x</span><span class="c003">.ml</span>, the <span class="c003">ocamlc</span>
compiler produces compiled object bytecode in the file <span class="c009">x</span><span class="c003">.cmo</span>.<p>If the interface file <span class="c009">x</span><span class="c003">.mli</span> exists, the implementation
<span class="c009">x</span><span class="c003">.ml</span> is checked against the corresponding compiled interface
<span class="c009">x</span><span class="c003">.cmi</span>, which is assumed to exist. If no interface
<span class="c009">x</span><span class="c003">.mli</span> is provided, the compilation of <span class="c009">x</span><span class="c003">.ml</span> produces a
compiled interface file <span class="c009">x</span><span class="c003">.cmi</span> in addition to the compiled
object code file <span class="c009">x</span><span class="c003">.cmo</span>. The file <span class="c009">x</span><span class="c003">.cmi</span> produced
corresponds to an interface that exports everything that is defined in
the implementation <span class="c009">x</span><span class="c003">.ml</span>.</p></li><li class="li-itemize">Arguments ending in <span class="c003">.cmo</span> are taken to be compiled object bytecode. These
files are linked together, along with the object files obtained
by compiling <span class="c003">.ml</span> arguments (if any), and the OCaml standard
library, to produce a standalone executable program. The order in
which <span class="c003">.cmo</span> and <span class="c003">.ml</span> arguments are presented on the command line is
relevant: compilation units are initialized in that order at
run-time, and it is a link-time error to use a component of a unit
before having initialized it. Hence, a given <span class="c009">x</span><span class="c003">.cmo</span> file must come
before all <span class="c003">.cmo</span> files that refer to the unit <span class="c009">x</span>.</li><li class="li-itemize">Arguments ending in <span class="c003">.cma</span> are taken to be libraries of object bytecode.
A library of object bytecode packs in a single file a set of object
bytecode files (<span class="c003">.cmo</span> files). Libraries are built with <span class="c003">ocamlc -a</span>
(see the description of the <span class="c003">-a</span> option below). The object files
contained in the library are linked as regular <span class="c003">.cmo</span> files (see
above), in the order specified when the <span class="c003">.cma</span> file was built. The
only difference is that if an object file contained in a library is
not referenced anywhere in the program, then it is not linked in.</li><li class="li-itemize">Arguments ending in <span class="c003">.c</span> are passed to the C compiler, which generates
a <span class="c003">.o</span> object file (<span class="c003">.obj</span> under Windows). This object file is linked
with the program if the <span class="c003">-custom</span> flag is set (see the description of
<span class="c003">-custom</span> below).</li><li class="li-itemize">Arguments ending in <span class="c003">.o</span> or <span class="c003">.a</span> (<span class="c003">.obj</span> or <span class="c003">.lib</span> under Windows)
are assumed to be C object files and libraries. They are passed to the
C linker when linking in <span class="c003">-custom</span> mode (see the description of
<span class="c003">-custom</span> below).</li><li class="li-itemize">Arguments ending in <span class="c003">.so</span> (<span class="c003">.dll</span> under Windows)
are assumed to be C shared libraries (DLLs). During linking, they are
searched for external C functions referenced from the OCaml code,
and their names are written in the generated bytecode executable.
The run-time system <span class="c003">ocamlrun</span> then loads them dynamically at program
start-up time.</li></ul><p>The output of the linking phase is a file containing compiled bytecode
that can be executed by the OCaml bytecode interpreter:
the command named <span class="c003">ocamlrun</span>. If <span class="c003">a.out</span> is the name of the file
produced by the linking phase, the command
</p><pre>
        ocamlrun a.out <span class="c009">arg</span><sub>1</sub> <span class="c009">arg</span><sub>2</sub> &#X2026; <span class="c009">arg</span><sub><span class="c009">n</span></sub>
</pre><p>
executes the compiled code contained in <span class="c003">a.out</span>, passing it as
arguments the character strings <span class="c009">arg</span><sub>1</sub> to <span class="c009">arg</span><sub><span class="c009">n</span></sub>.
(See chapter&#XA0;<a href="runtime.html#c%3Aruntime">11</a> for more details.)</p><p>On most systems, the file produced by the linking
phase can be run directly, as in:
</p><pre>
        ./a.out <span class="c009">arg</span><sub>1</sub> <span class="c009">arg</span><sub>2</sub> &#X2026; <span class="c009">arg</span><sub><span class="c009">n</span></sub>
</pre><p>
The produced file has the executable bit set, and it manages to launch
the bytecode interpreter by itself.</p>
<h2 class="section" id="sec281">9.2&#XA0;&#XA0;Options</h2>
<p><a id="s:comp-options"></a></p><p>The following command-line options are recognized by <span class="c003">ocamlc</span>.
The options <span class="c003">-pack</span>, <span class="c003">-a</span>, <span class="c003">-c</span> and <span class="c003">-output-obj</span> are mutually exclusive.
</p><dl class="description"><dt class="dt-description">
<span class="c006">-a</span></dt><dd class="dd-description">
Build a library(<span class="c003">.cma</span> file)
with the object files ( <span class="c003">.cmo</span> files)
given on the command line, instead of linking them into an executable file.
The name of the library must be set with the <span class="c003">-o</span> option.<p>If <span class="c003">-custom</span>, <span class="c003">-cclib</span> or <span class="c003">-ccopt</span> options are passed on the command
line, these options are stored in the resulting <span class="c003">.cma</span>library. Then,
linking with this library automatically adds back the <span class="c003">-custom</span>, 
<span class="c003">-cclib</span> and <span class="c003">-ccopt</span> options as if they had been provided on the
command line, unless the <span class="c003">-noautolink</span> option is given.
</p></dd><dt class="dt-description"><span class="c006">-absname</span></dt><dd class="dd-description">
Force error messages to show absolute paths for file names.</dd><dt class="dt-description"><span class="c006">-annot</span></dt><dd class="dd-description">
Dump detailed information about the compilation (types, bindings,
tail-calls, etc). The information for file <span class="c009">src</span><span class="c003">.ml</span>
is put into file <span class="c009">src</span><span class="c003">.annot</span>. In case of a type error, dump
all the information inferred by the type-checker before the error.
The <span class="c009">src</span><span class="c003">.annot</span> file can be used with the emacs commands given in
<span class="c003">emacs/caml-types.el</span> to display types and other annotations
interactively.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-args</span> <span class="c009">filename</span></span></dt><dd class="dd-description">
Read additional newline-terminated command line arguments from <span class="c009">filename</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-args0</span> <span class="c009">filename</span></span></dt><dd class="dd-description">
Read additional null character terminated command line arguments from <span class="c009">filename</span>.
</dd><dt class="dt-description"><span class="c006">-bin-annot</span></dt><dd class="dd-description">
Dump detailed information about the compilation (types, bindings,
tail-calls, etc) in binary format. The information for file <span class="c009">src</span><span class="c003">.ml</span>
(resp. <span class="c009">src</span><span class="c003">.mli</span>) is put into file <span class="c009">src</span><span class="c003">.cmt</span>
(resp. <span class="c009">src</span><span class="c003">.cmti</span>). In case of a type error, dump
all the information inferred by the type-checker before the error.
The <span class="c003">*.cmt</span> and <span class="c003">*.cmti</span> files produced by <span class="c003">-bin-annot</span> contain
more information and are much more compact than the files produced by
<span class="c003">-annot</span>.
</dd><dt class="dt-description"><span class="c006">-c</span></dt><dd class="dd-description">
Compile only. Suppress the linking phase of the
compilation. Source code files are turned into compiled files, but no
executable file is produced. This option is useful to
compile modules separately.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-cc</span> <span class="c009">ccomp</span></span></dt><dd class="dd-description">
Use <span class="c009">ccomp</span> as the C linker 
when linking in &#X201C;custom runtime&#X201D; mode (see the <span class="c003">-custom</span> option)
and as the C compiler for compiling <span class="c003">.c</span> source files.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-cclib</span> <span class="c003">-l</span><span class="c009">libname</span></span></dt><dd class="dd-description">
Pass the <span class="c003">-l</span><span class="c009">libname</span> option to the C linker
when linking in &#X201C;custom runtime&#X201D; mode (see the <span class="c003">-custom</span> option).
This causes the given C library to be linked with the program.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-ccopt</span> <span class="c009">option</span></span></dt><dd class="dd-description">
Pass the given option to the C compiler and linker.
When linking in &#X201C;custom runtime&#X201D; mode, for instance<span class="c003">-ccopt -L</span><span class="c009">dir</span> causes the C linker to search for C libraries in
directory <span class="c009">dir</span>.(See the <span class="c003">-custom</span> option.)
</dd><dt class="dt-description"><span class="c013"><span class="c003">-color</span> <span class="c009">mode</span></span></dt><dd class="dd-description">
Enable or disable colors in compiler messages (especially warnings and errors).
The following modes are supported:
<dl class="description"><dt class="dt-description">
<span class="c006">auto</span></dt><dd class="dd-description"> use heuristics to enable colors only if the output supports them (an ANSI-compatible tty terminal);
</dd><dt class="dt-description"><span class="c006">always</span></dt><dd class="dd-description"> enable colors unconditionally;
</dd><dt class="dt-description"><span class="c006">never</span></dt><dd class="dd-description"> disable color output.
</dd></dl>
The default setting is &#X2019;auto&#X2019;, and the current heuristic
checks that the <span class="c003">TERM</span> environment variable exists and is
not empty or <span class="c003">dumb</span>, and that &#X2019;isatty(stderr)&#X2019; holds.<p>The environment variable <span class="c003">OCAML_COLOR</span> is considered if <span class="c003">-color</span> is not
provided. Its values are auto/always/never as above.
</p></dd><dt class="dt-description"><span class="c006">-compat-32</span></dt><dd class="dd-description">
Check that the generated bytecode executable can run on 32-bit
platforms and signal an error if it cannot. This is useful when
compiling bytecode on a 64-bit machine.
</dd><dt class="dt-description"><span class="c006">-config</span></dt><dd class="dd-description">
Print the version number of <span class="c003">ocamlc</span> and a detailed
summary of its configuration, then exit.</dd><dt class="dt-description"><span class="c006">-custom</span></dt><dd class="dd-description">
Link in &#X201C;custom runtime&#X201D; mode. In the default linking mode, the
linker produces bytecode that is intended to be executed with the
shared runtime system, <span class="c003">ocamlrun</span>. In the custom runtime mode, the
linker produces an output file that contains both the runtime system
and the bytecode for the program. The resulting file is larger, but it
can be executed directly, even if the <span class="c003">ocamlrun</span> command is not
installed. Moreover, the &#X201C;custom runtime&#X201D; mode enables static
linking of OCaml code with user-defined C functions, as described in
chapter&#XA0;<a href="intfc.html#c%3Aintf-c">20</a>.
<blockquote class="quote"><span class="c007">Unix:</span>&#XA0;&#XA0;
Never use the <span class="c003">strip</span> command on executables produced by <span class="c003">ocamlc -custom</span>,
this would remove the bytecode part of the executable.
</blockquote>
<blockquote class="quote"><span class="c007">Unix:</span>&#XA0;&#XA0;
Security warning: never set the &#X201C;setuid&#X201D; or &#X201C;setgid&#X201D; bits on executables
produced by <span class="c003">ocamlc -custom</span>, this would make them vulnerable to attacks.
</blockquote>
</dd><dt class="dt-description"><span class="c013"><span class="c003">-depend</span> <span class="c009">ocamldep-args</span></span></dt><dd class="dd-description">
Compute dependencies, as the <span class="c003">ocamldep</span> command would do. The remaining
arguments are interpreted as if they were given to the <span class="c003">ocamldep</span> command.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-dllib</span> <span class="c003">-l</span><span class="c009">libname</span></span></dt><dd class="dd-description">
Arrange for the C shared library <span class="c003">dll</span><span class="c009">libname</span><span class="c003">.so</span>
(<span class="c003">dll</span><span class="c009">libname</span><span class="c003">.dll</span> under Windows) to be loaded dynamically
by the run-time system <span class="c003">ocamlrun</span> at program start-up time.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-dllpath</span> <span class="c009">dir</span></span></dt><dd class="dd-description">
Adds the directory <span class="c009">dir</span> to the run-time search path for shared
C libraries. At link-time, shared libraries are searched in the
standard search path (the one corresponding to the <span class="c003">-I</span> option).
The <span class="c003">-dllpath</span> option simply stores <span class="c009">dir</span> in the produced
executable file, where <span class="c003">ocamlrun</span> can find it and use it as
described in section&#XA0;<a href="runtime.html#s-ocamlrun-dllpath">11.3</a>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-for-pack</span> <span class="c009">module-path</span></span></dt><dd class="dd-description">
Generate an object file (<span class="c003">.cmo</span>)
that can later be included
as a sub-module (with the given access path) of a compilation unit
constructed with <span class="c003">-pack</span>. For instance,
<span class="c003">ocamlc -for-pack P -c A.ml</span>
will generate <span class="c003">a..cmo</span> that can
later be used with <span class="c003">ocamlc -pack -o P.cmo a.cmo</span>.
Note: you can still pack a module that was compiled without
<span class="c003">-for-pack</span> but in this case exceptions will be printed with the wrong
names.
</dd><dt class="dt-description"><span class="c006">-g</span></dt><dd class="dd-description">
Add debugging information while compiling and linking. This option is
required in order to be able to debug the program with <span class="c003">ocamldebug</span>
(see chapter&#XA0;<a href="debugger.html#c%3Adebugger">17</a>), and to produce stack backtraces when
the program terminates on an uncaught exception (see
section&#XA0;<a href="runtime.html#ocamlrun-options">11.2</a>).
</dd><dt class="dt-description"><span class="c006">-i</span></dt><dd class="dd-description">
Cause the compiler to print all defined names (with their inferred
types or their definitions) when compiling an implementation (<span class="c003">.ml</span>
file). No compiled files (<span class="c003">.cmo</span> and <span class="c003">.cmi</span> files) are produced.
This can be useful to check the types inferred by the
compiler. Also, since the output follows the syntax of interfaces, it
can help in writing an explicit interface (<span class="c003">.mli</span> file) for a file:
just redirect the standard output of the compiler to a <span class="c003">.mli</span> file,
and edit that file to remove all declarations of unexported names.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-I</span> <span class="c009">directory</span></span></dt><dd class="dd-description">
Add the given directory to the list of directories searched for

compiled interface files (<span class="c003">.cmi</span>), compiled object code files <span class="c003">.cmo</span>,
libraries (<span class="c003">.cma</span>) and C libraries specified with <span class="c003">-cclib -lxxx</span>.

By default, the current directory is searched first, then the standard
library directory. Directories added with <span class="c003">-I</span> are searched after the
current directory, in the order in which they were given on the command line,
but before the standard library directory. See also option <span class="c003">-nostdlib</span>.<p>If the given directory starts with <span class="c003">+</span>, it is taken relative to the
standard library directory. For instance, <span class="c003">-I +unix</span> adds the
subdirectory <span class="c003">unix</span> of the standard library to the search path.</p></dd><dt class="dt-description"><span class="c013"><span class="c003">-impl</span> <span class="c009">filename</span></span></dt><dd class="dd-description">
Compile the file <span class="c009">filename</span> as an implementation file, even if its
extension is not <span class="c003">.ml</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-intf</span> <span class="c009">filename</span></span></dt><dd class="dd-description">
Compile the file <span class="c009">filename</span> as an interface file, even if its
extension is not <span class="c003">.mli</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-intf-suffix</span> <span class="c009">string</span></span></dt><dd class="dd-description">
Recognize file names ending with <span class="c009">string</span> as interface files
(instead of the default <span class="c003">.mli</span>).
</dd><dt class="dt-description"><span class="c006">-labels</span></dt><dd class="dd-description">
Labels are not ignored in types, labels may be used in applications,
and labelled parameters can be given in any order. This is the default.</dd><dt class="dt-description"><span class="c006">-linkall</span></dt><dd class="dd-description">
Force all modules contained in libraries to be linked in. If this
flag is not given, unreferenced modules are not linked in. When
building a library (option <span class="c003">-a</span>), setting the <span class="c003">-linkall</span> option forces all
subsequent links of programs involving that library to link all the
modules contained in the library. When compiling a module (option
<span class="c003">-c</span>), setting the <span class="c003">-linkall</span> option ensures that this module will
always be linked if it is put in a library and this library is linked.
</dd><dt class="dt-description"><span class="c006">-make-runtime</span></dt><dd class="dd-description">
Build a custom runtime system (in the file specified by option <span class="c003">-o</span>)
incorporating the C object files and libraries given on the command
line. This custom runtime system can be used later to execute
bytecode executables produced with the
<span class="c003">ocamlc -use-runtime</span> <span class="c009">runtime-name</span> option.
See section&#XA0;<a href="intfc.html#s%3Acustom-runtime">20.1.6</a> for more information.
</dd><dt class="dt-description"><span class="c006">-no-alias-deps</span></dt><dd class="dd-description">
Do not record dependencies for module aliases. See
section&#XA0;<a href="extn.html#s%3Amodule-alias">8.12</a> for more information.
</dd><dt class="dt-description"><span class="c006">-no-app-funct</span></dt><dd class="dd-description">
Deactivates the applicative behaviour of functors. With this option,
each functor application generates new types in its result and
applying the same functor twice to the same argument yields two
incompatible structures.</dd><dt class="dt-description"><span class="c006">-noassert</span></dt><dd class="dd-description">
Do not compile assertion checks. Note that the special form
<span class="c003">assert false</span> is always compiled because it is typed specially.
This flag has no effect when linking already-compiled files.</dd><dt class="dt-description"><span class="c006">-noautolink</span></dt><dd class="dd-description">
When linking <span class="c003">.cma</span>libraries, ignore <span class="c003">-custom</span>, <span class="c003">-cclib</span> and <span class="c003">-ccopt</span>
options potentially contained in the libraries (if these options were
given when building the libraries). This can be useful if a library
contains incorrect specifications of C libraries or C options; in this
case, during linking, set <span class="c003">-noautolink</span> and pass the correct C
libraries and options on the command line.
</dd><dt class="dt-description"><span class="c006">-nolabels</span></dt><dd class="dd-description">
Ignore non-optional labels in types. Labels cannot be used in
applications, and parameter order becomes strict.</dd><dt class="dt-description"><span class="c006">-nostdlib</span></dt><dd class="dd-description">
Do not include the standard library directory in the list of
directories searched for
compiled interface files (<span class="c003">.cmi</span>), compiled object code files
(<span class="c003">.cmo</span>), libraries (<span class="c003">.cma</span>), and C libraries specified with
<span class="c003">-cclib -lxxx</span>. See also option <span class="c003">-I</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-o</span> <span class="c009">exec-file</span></span></dt><dd class="dd-description">
Specify the name of the output file produced by the
compiler. The
default output name is <span class="c003">a.out</span> under Unix and <span class="c003">camlprog.exe</span> under
Windows. If the <span class="c003">-a</span> option is given, specify the name of the library
produced. If the <span class="c003">-pack</span> option is given, specify the name of the
packed object file produced. If the <span class="c003">-output-obj</span> option is given,
specify the name of the output file produced.

If the <span class="c003">-c</span> option is given, specify the name of the object
file produced for the <em>next</em> source file that appears on the
command line.
</dd><dt class="dt-description"><span class="c006">-opaque</span></dt><dd class="dd-description">
When the native compiler compiles an implementation, by default it
produces a <span class="c003">.cmx</span> file containing information for cross-module
optimization. It also expects <span class="c003">.cmx</span> files to be present for the
dependencies of the currently compiled source, and uses them for
optimization. Since OCaml 4.03, the compiler will emit a warning if it
is unable to locate the <span class="c003">.cmx</span> file of one of those dependencies.<p>The <span class="c003">-opaque</span> option, available since 4.04, disables cross-module
optimization information for the currently compiled unit. When
compiling <span class="c003">.mli</span> interface, using <span class="c003">-opaque</span> marks the compiled <span class="c003">.cmi</span>
interface so that subsequent compilations of modules that depend on it
will not rely on the corresponding <span class="c003">.cmx</span> file, nor warn if it is
absent. When the native compiler compiles a <span class="c003">.ml</span> implementation,
using <span class="c003">-opaque</span> generates a <span class="c003">.cmx</span> that does not contain any
cross-module optimization information.</p><p>Using this option may degrade the quality of generated code, but it
reduces compilation time, both on clean and incremental
builds. Indeed, with the native compiler, when the implementation of
a compilation unit changes, all the units that depend on it may need
to be recompiled &#X2013; because the cross-module information may have
changed. If the compilation unit whose implementation changed was
compiled with <span class="c003">-opaque</span>, no such recompilation needs to occur. This
option can thus be used, for example, to get faster edit-compile-test
feedback loops.
</p></dd><dt class="dt-description"><span class="c013"><span class="c003">-open</span> <span class="c009">Module</span></span></dt><dd class="dd-description">
Opens the given module before processing the interface or
implementation files. If several <span class="c003">-open</span> options are given,
they are processed in order, just as if
the statements <span class="c003">open!</span> <span class="c009">Module1</span><span class="c003">;;</span> <span class="c003">...</span> <span class="c003">open!</span> <span class="c009">ModuleN</span><span class="c003">;;</span>
were added at the top of each file.
</dd><dt class="dt-description"><span class="c006">-output-obj</span></dt><dd class="dd-description">
Cause the linker to produce a C object file instead of
a bytecode executable file.
This is useful to wrap OCaml code as a C library,
callable from any C program. See chapter&#XA0;<a href="intfc.html#c%3Aintf-c">20</a>,
section&#XA0;<a href="intfc.html#s%3Aembedded-code">20.7.5</a>. The name of the output object file
must be set with the <span class="c003">-o</span> option.
This option can also be used to produce a C source file (<span class="c003">.c</span> extension) or
a compiled shared/dynamic library (<span class="c003">.so</span> extension, <span class="c003">.dll</span> under Windows).
</dd><dt class="dt-description"><span class="c006">-pack</span></dt><dd class="dd-description">
Build a bytecode object file (<span class="c003">.cmo</span> file) and its associated compiled
interface (<span class="c003">.cmi</span>) that combines the object
files given on the command line, making them appear as sub-modules of
the output <span class="c003">.cmo</span> file. The name of the output <span class="c003">.cmo</span> file must be
given with the <span class="c003">-o</span> option. For instance,
<pre>        ocamlc -pack -o p.cmo a.cmo b.cmo c.cmo
</pre>generates compiled files <span class="c003">p.cmo</span> and <span class="c003">p.cmi</span> describing a compilation
unit having three sub-modules <span class="c003">A</span>, <span class="c003">B</span> and <span class="c003">C</span>, corresponding to the
contents of the object files <span class="c003">a.cmo</span>, <span class="c003">b.cmo</span> and <span class="c003">c.cmo</span>. These
contents can be referenced as <span class="c003">P.A</span>, <span class="c003">P.B</span> and <span class="c003">P.C</span> in the remainder
of the program.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-plugin</span> <span class="c009">plugin</span></span></dt><dd class="dd-description">
Dynamically load the code of the given <span class="c009">plugin</span>
(a <span class="c003">.cmo</span>, <span class="c003">.cma</span> or <span class="c003">.cmxs</span> file) in the compiler. <span class="c009">plugin</span> must exist in
the same kind of code as the compiler (<span class="c003">ocamlc.byte</span> must load bytecode
plugins, while <span class="c003">ocamlc.opt</span> must load native code plugins), and
extension adaptation is done automatically for <span class="c003">.cma</span> files (to <span class="c003">.cmxs</span> files
if the compiler is compiled in native code).
</dd><dt class="dt-description"><span class="c013"><span class="c003">-pp</span> <span class="c009">command</span></span></dt><dd class="dd-description">
Cause the compiler to call the given <span class="c009">command</span> as a preprocessor
for each source file. The output of <span class="c009">command</span> is redirected to
an intermediate file, which is compiled. If there are no compilation
errors, the intermediate file is deleted afterwards.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-ppx</span> <span class="c009">command</span></span></dt><dd class="dd-description">
After parsing, pipe the abstract syntax tree through the preprocessor
<span class="c009">command</span>. The module <span class="c003">Ast_mapper</span>, described in
chapter&#XA0;<a href="parsing.html#c%3Aparsinglib">27</a>: <a href="libref/Ast_mapper.html"> <span class="c003">Ast_mapper</span> </a>
,
implements the external interface of a preprocessor.</dd><dt class="dt-description"><span class="c006">-principal</span></dt><dd class="dd-description">
Check information path during type-checking, to make sure that all
types are derived in a principal way. When using labelled arguments
and/or polymorphic methods, this flag is required to ensure future
versions of the compiler will be able to infer types correctly, even
if internal algorithms change.
All programs accepted in <span class="c003">-principal</span> mode are also accepted in the
default mode with equivalent types, but different binary signatures,
and this may slow down type checking; yet it is a good idea to
use it once before publishing source code.</dd><dt class="dt-description"><span class="c006">-rectypes</span></dt><dd class="dd-description">
Allow arbitrary recursive types during type-checking. By default,
only recursive types where the recursion goes through an object type
are supported.Note that once you have created an interface using this
flag, you must use it again for all dependencies.</dd><dt class="dt-description"><span class="c013"><span class="c003">-runtime-variant</span> <span class="c009">suffix</span></span></dt><dd class="dd-description">
Add the <span class="c009">suffix</span> string to the name of the runtime library used by
the program. Currently, only one such suffix is supported: <span class="c003">d</span>, and
only if the OCaml compiler was configured with option
<span class="c003">-with-debug-runtime</span>. This suffix gives the debug version of the
runtime, which is useful for debugging pointer problems in low-level
code such as C stubs.
</dd><dt class="dt-description"><span class="c006">-safe-string</span></dt><dd class="dd-description">
Enforce the separation between types <span class="c003">string</span> and <span class="c003">bytes</span>,
thereby making strings read-only. This will become the default in
a future version of OCaml.</dd><dt class="dt-description"><span class="c006">-short-paths</span></dt><dd class="dd-description">
When a type is visible under several module-paths, use the shortest
one when printing the type&#X2019;s name in inferred interfaces and error and
warning messages. Identifier names starting with an underscore <span class="c003">_</span> or
containing double underscores <span class="c003">__</span> incur a penalty of +10 when computing
their length.</dd><dt class="dt-description"><span class="c006">-strict-sequence</span></dt><dd class="dd-description">
Force the left-hand part of each sequence to have type unit.</dd><dt class="dt-description"><span class="c006">-strict-formats</span></dt><dd class="dd-description">
Reject invalid formats that were accepted in legacy format
implementations. You should use this flag to detect and fix such
invalid formats, as they will be rejected by future OCaml versions.</dd><dt class="dt-description"><span class="c006">-thread</span></dt><dd class="dd-description">
Compile or link multithreaded programs, in combination with the
system <span class="c003">threads</span> library described in chapter&#XA0;<a href="libthreads.html#c%3Athreads">31</a>.
</dd><dt class="dt-description"><span class="c006">-unboxed-types</span></dt><dd class="dd-description">
When a type is unboxable (i.e. a record with a single argument or a
concrete datatype with a single constructor of one argument) it will
be unboxed unless annotated with <span class="c003">[@@ocaml.boxed]</span>.
</dd><dt class="dt-description"><span class="c006">-no-unboxed-types</span></dt><dd class="dd-description">
When a type is unboxable it will be boxed unless annotated with
<span class="c003">[@@ocaml.unboxed]</span>. This is the default.
</dd><dt class="dt-description"><span class="c006">-unsafe</span></dt><dd class="dd-description">
Turn bound checking off for array and string accesses (the <span class="c003">v.(i)</span> and
<span class="c003">s.[i]</span> constructs). Programs compiled with <span class="c003">-unsafe</span> are therefore
slightly faster, but unsafe: anything can happen if the program
accesses an array or string outside of its bounds.
Additionally, turn off the check for zero divisor in integer division
and modulus operations. With <span class="c003">-unsafe</span>, an integer division
(or modulus) by zero can halt the program or continue with an
unspecified result instead of raising a <span class="c003">Division_by_zero</span> exception.
</dd><dt class="dt-description"><span class="c006">-unsafe-string</span></dt><dd class="dd-description">
Identify the types <span class="c003">string</span> and <span class="c003">bytes</span>,
thereby making strings writable. For reasons of backward compatibility,
this is the default setting for the moment, but this will change in a future
version of OCaml.</dd><dt class="dt-description"><span class="c013"><span class="c003">-use-runtime</span> <span class="c009">runtime-name</span></span></dt><dd class="dd-description">
Generate a bytecode executable file that can be executed on the custom
runtime system <span class="c009">runtime-name</span>, built earlier with
<span class="c003">ocamlc -make-runtime</span> <span class="c009">runtime-name</span>.
See section&#XA0;<a href="intfc.html#s%3Acustom-runtime">20.1.6</a> for more information.
</dd><dt class="dt-description"><span class="c006">-v</span></dt><dd class="dd-description">
Print the version number of the compiler and the location of the
standard library directory, then exit.</dd><dt class="dt-description"><span class="c006">-verbose</span></dt><dd class="dd-description">
Print all external commands before they are executed,

in particular invocations of the C compiler and linker in <span class="c003">-custom</span> mode.
Useful to debug C library problems.</dd><dt class="dt-description"><span class="c006">-vmthread</span></dt><dd class="dd-description">
Compile or link multithreaded programs, in combination with the
VM-level <span class="c003">threads</span> library described in chapter&#XA0;<a href="libthreads.html#c%3Athreads">31</a>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-version</span> or <span class="c003">-vnum</span></span></dt><dd class="dd-description">
Print the version number of the compiler in short form (e.g. <span class="c003">3.11.0</span>),
then exit.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-w</span> <span class="c009">warning-list</span></span></dt><dd class="dd-description">
Enable, disable, or mark as fatal the warnings specified by the argument
<span class="c009">warning-list</span>.
Each warning can be <em>enabled</em> or <em>disabled</em>, and each warning
can be <em>fatal</em> or <em>non-fatal</em>.
If a warning is disabled, it isn&#X2019;t displayed and doesn&#X2019;t affect
compilation in any way (even if it is fatal). If a warning is
enabled, it is displayed normally by the compiler whenever the source
code triggers it. If it is enabled and fatal, the compiler will also
stop with an error after displaying it.<p>The <span class="c009">warning-list</span> argument is a sequence of warning specifiers,
with no separators between them. A warning specifier is one of the
following:</p><dl class="description"><dt class="dt-description">
<span class="c013"><span class="c003">+</span><span class="c009">num</span></span></dt><dd class="dd-description"> Enable warning number <span class="c009">num</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-</span><span class="c009">num</span></span></dt><dd class="dd-description"> Disable warning number <span class="c009">num</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">@</span><span class="c009">num</span></span></dt><dd class="dd-description"> Enable and mark as fatal warning number <span class="c009">num</span>.
</dd><dt class="dt-description"><span class="c013"><span class="c003">+</span><span class="c009">num1</span>..<span class="c009">num2</span></span></dt><dd class="dd-description"> Enable warnings in the given range.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-</span><span class="c009">num1</span>..<span class="c009">num2</span></span></dt><dd class="dd-description"> Disable warnings in the given range.
</dd><dt class="dt-description"><span class="c013"><span class="c003">@</span><span class="c009">num1</span>..<span class="c009">num2</span></span></dt><dd class="dd-description"> Enable and mark as fatal warnings in
the given range.
</dd><dt class="dt-description"><span class="c013"><span class="c003">+</span><span class="c009">letter</span></span></dt><dd class="dd-description"> Enable the set of warnings corresponding to
<span class="c009">letter</span>. The letter may be uppercase or lowercase.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-</span><span class="c009">letter</span></span></dt><dd class="dd-description"> Disable the set of warnings corresponding to
<span class="c009">letter</span>. The letter may be uppercase or lowercase.
</dd><dt class="dt-description"><span class="c013"><span class="c003">@</span><span class="c009">letter</span></span></dt><dd class="dd-description"> Enable and mark as fatal the set of warnings
corresponding to <span class="c009">letter</span>. The letter may be uppercase or
lowercase.
</dd><dt class="dt-description"><span class="c011">uppercase-letter</span></dt><dd class="dd-description"> Enable the set of warnings corresponding
to <span class="c009">uppercase-letter</span>.
</dd><dt class="dt-description"><span class="c011">lowercase-letter</span></dt><dd class="dd-description"> Disable the set of warnings corresponding
to <span class="c009">lowercase-letter</span>.
</dd></dl><p>Warning numbers and letters which are out of the range of warnings
that are currently defined are ignored. The warnings are as follows.
</p><dl class="description"><dt class="dt-description">
<span class="c013">1</span></dt><dd class="dd-description"> Suspicious-looking start-of-comment mark.
</dd><dt class="dt-description"><span class="c013">2</span></dt><dd class="dd-description"> Suspicious-looking end-of-comment mark.
</dd><dt class="dt-description"><span class="c013">3</span></dt><dd class="dd-description"> Deprecated feature.
</dd><dt class="dt-description"><span class="c013">4</span></dt><dd class="dd-description"> Fragile pattern matching: matching that will remain complete even
if additional constructors are added to one of the variant types
matched.
</dd><dt class="dt-description"><span class="c013">5</span></dt><dd class="dd-description"> Partially applied function: expression whose result has function
type and is ignored.
</dd><dt class="dt-description"><span class="c013">6</span></dt><dd class="dd-description"> Label omitted in function application.
</dd><dt class="dt-description"><span class="c013">7</span></dt><dd class="dd-description"> Method overridden.
</dd><dt class="dt-description"><span class="c013">8</span></dt><dd class="dd-description"> Partial match: missing cases in pattern-matching.
</dd><dt class="dt-description"><span class="c013">9</span></dt><dd class="dd-description"> Missing fields in a record pattern.
</dd><dt class="dt-description"><span class="c013">10</span></dt><dd class="dd-description"> Expression on the left-hand side of a sequence that doesn&#X2019;t have type
<span class="c003">unit</span> (and that is not a function, see warning number 5).
</dd><dt class="dt-description"><span class="c013">11</span></dt><dd class="dd-description"> Redundant case in a pattern matching (unused match case).
</dd><dt class="dt-description"><span class="c013">12</span></dt><dd class="dd-description"> Redundant sub-pattern in a pattern-matching.
</dd><dt class="dt-description"><span class="c013">13</span></dt><dd class="dd-description"> Instance variable overridden.
</dd><dt class="dt-description"><span class="c013">14</span></dt><dd class="dd-description"> Illegal backslash escape in a string constant.
</dd><dt class="dt-description"><span class="c013">15</span></dt><dd class="dd-description"> Private method made public implicitly.
</dd><dt class="dt-description"><span class="c013">16</span></dt><dd class="dd-description"> Unerasable optional argument.
</dd><dt class="dt-description"><span class="c013">17</span></dt><dd class="dd-description"> Undeclared virtual method.
</dd><dt class="dt-description"><span class="c013">18</span></dt><dd class="dd-description"> Non-principal type.
</dd><dt class="dt-description"><span class="c013">19</span></dt><dd class="dd-description"> Type without principality.
</dd><dt class="dt-description"><span class="c013">20</span></dt><dd class="dd-description"> Unused function argument.
</dd><dt class="dt-description"><span class="c013">21</span></dt><dd class="dd-description"> Non-returning statement.
</dd><dt class="dt-description"><span class="c013">22</span></dt><dd class="dd-description"> Preprocessor warning.
</dd><dt class="dt-description"><span class="c013">23</span></dt><dd class="dd-description"> Useless record <span class="c003">with</span> clause.
</dd><dt class="dt-description"><span class="c013">24</span></dt><dd class="dd-description"> Bad module name: the source file name is not a valid OCaml module name.
</dd><dt class="dt-description"><span class="c013">25</span></dt><dd class="dd-description"> Deprecated: now part of warning 8.
</dd><dt class="dt-description"><span class="c013">26</span></dt><dd class="dd-description"> Suspicious unused variable: unused variable that is bound
with <span class="c003">let</span> or <span class="c003">as</span>, and doesn&#X2019;t start with an underscore (<span class="c003">_</span>)
character.
</dd><dt class="dt-description"><span class="c013">27</span></dt><dd class="dd-description"> Innocuous unused variable: unused variable that is not bound with
<span class="c003">let</span> nor <span class="c003">as</span>, and doesn&#X2019;t start with an underscore (<span class="c003">_</span>)
character.
</dd><dt class="dt-description"><span class="c013">28</span></dt><dd class="dd-description"> Wildcard pattern given as argument to a constant constructor.
</dd><dt class="dt-description"><span class="c013">29</span></dt><dd class="dd-description"> Unescaped end-of-line in a string constant (non-portable code).
</dd><dt class="dt-description"><span class="c013">30</span></dt><dd class="dd-description"> Two labels or constructors of the same name are defined in two
mutually recursive types.
</dd><dt class="dt-description"><span class="c013">31</span></dt><dd class="dd-description"> A module is linked twice in the same executable.
</dd><dt class="dt-description"><span class="c013">32</span></dt><dd class="dd-description"> Unused value declaration.
</dd><dt class="dt-description"><span class="c013">33</span></dt><dd class="dd-description"> Unused open statement.
</dd><dt class="dt-description"><span class="c013">34</span></dt><dd class="dd-description"> Unused type declaration.
</dd><dt class="dt-description"><span class="c013">35</span></dt><dd class="dd-description"> Unused for-loop index.
</dd><dt class="dt-description"><span class="c013">36</span></dt><dd class="dd-description"> Unused ancestor variable.
</dd><dt class="dt-description"><span class="c013">37</span></dt><dd class="dd-description"> Unused constructor.
</dd><dt class="dt-description"><span class="c013">38</span></dt><dd class="dd-description"> Unused extension constructor.
</dd><dt class="dt-description"><span class="c013">39</span></dt><dd class="dd-description"> Unused rec flag.
</dd><dt class="dt-description"><span class="c013">40</span></dt><dd class="dd-description"> Constructor or label name used out of scope.
</dd><dt class="dt-description"><span class="c013">41</span></dt><dd class="dd-description"> Ambiguous constructor or label name.
</dd><dt class="dt-description"><span class="c013">42</span></dt><dd class="dd-description"> Disambiguated constructor or label name (compatibility warning).
</dd><dt class="dt-description"><span class="c013">43</span></dt><dd class="dd-description"> Nonoptional label applied as optional.
</dd><dt class="dt-description"><span class="c013">44</span></dt><dd class="dd-description"> Open statement shadows an already defined identifier.
</dd><dt class="dt-description"><span class="c013">45</span></dt><dd class="dd-description"> Open statement shadows an already defined label or constructor.
</dd><dt class="dt-description"><span class="c013">46</span></dt><dd class="dd-description"> Error in environment variable.
</dd><dt class="dt-description"><span class="c013">47</span></dt><dd class="dd-description"> Illegal attribute payload.
</dd><dt class="dt-description"><span class="c013">48</span></dt><dd class="dd-description"> Implicit elimination of optional arguments.
</dd><dt class="dt-description"><span class="c013">49</span></dt><dd class="dd-description"> Absent cmi file when looking up module alias.
</dd><dt class="dt-description"><span class="c013">50</span></dt><dd class="dd-description"> Unexpected documentation comment.
</dd><dt class="dt-description"><span class="c013">51</span></dt><dd class="dd-description"> Warning on non-tail calls if @tailcall present.
</dd><dt class="dt-description"><span class="c013">52 (see </span><a href="#ss%3Awarn52"><span class="c013">9.5.2</span></a><span class="c013">)</span></dt><dd class="dd-description"> Fragile constant pattern.
</dd><dt class="dt-description"><span class="c013">53</span></dt><dd class="dd-description"> Attribute cannot appear in this context
</dd><dt class="dt-description"><span class="c013">54</span></dt><dd class="dd-description"> Attribute used more than once on an expression
</dd><dt class="dt-description"><span class="c013">55</span></dt><dd class="dd-description"> Inlining impossible
</dd><dt class="dt-description"><span class="c013">56</span></dt><dd class="dd-description"> Unreachable case in a pattern-matching (based on type information).
</dd><dt class="dt-description"><span class="c013">57 (see </span><a href="#ss%3Awarn57"><span class="c013">9.5.3</span></a><span class="c013">)</span></dt><dd class="dd-description"> Ambiguous or-pattern variables under guard
</dd><dt class="dt-description"><span class="c013">58</span></dt><dd class="dd-description"> Missing cmx file
</dd><dt class="dt-description"><span class="c013">59</span></dt><dd class="dd-description"> Assignment to non-mutable value
</dd><dt class="dt-description"><span class="c013">60</span></dt><dd class="dd-description"> Unused module declaration
</dd><dt class="dt-description"><span class="c013">61</span></dt><dd class="dd-description"> Unboxable type in primitive declaration
</dd><dt class="dt-description"><span class="c013">62</span></dt><dd class="dd-description"> Type constraint on GADT type declaration
</dd><dt class="dt-description"><span class="c013">A</span></dt><dd class="dd-description"> all warnings
</dd><dt class="dt-description"><span class="c013">C</span></dt><dd class="dd-description"> warnings 1, 2.
</dd><dt class="dt-description"><span class="c013">D</span></dt><dd class="dd-description"> Alias for warning 3.
</dd><dt class="dt-description"><span class="c013">E</span></dt><dd class="dd-description"> Alias for warning 4.
</dd><dt class="dt-description"><span class="c013">F</span></dt><dd class="dd-description"> Alias for warning 5.
</dd><dt class="dt-description"><span class="c013">K</span></dt><dd class="dd-description"> warnings 32, 33, 34, 35, 36, 37, 38, 39.
</dd><dt class="dt-description"><span class="c013">L</span></dt><dd class="dd-description"> Alias for warning 6.
</dd><dt class="dt-description"><span class="c013">M</span></dt><dd class="dd-description"> Alias for warning 7.
</dd><dt class="dt-description"><span class="c013">P</span></dt><dd class="dd-description"> Alias for warning 8.
</dd><dt class="dt-description"><span class="c013">R</span></dt><dd class="dd-description"> Alias for warning 9.
</dd><dt class="dt-description"><span class="c013">S</span></dt><dd class="dd-description"> Alias for warning 10.
</dd><dt class="dt-description"><span class="c013">U</span></dt><dd class="dd-description"> warnings 11, 12.
</dd><dt class="dt-description"><span class="c013">V</span></dt><dd class="dd-description"> Alias for warning 13.
</dd><dt class="dt-description"><span class="c013">X</span></dt><dd class="dd-description"> warnings 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 30.
</dd><dt class="dt-description"><span class="c013">Y</span></dt><dd class="dd-description"> Alias for warning 26.
</dd><dt class="dt-description"><span class="c013">Z</span></dt><dd class="dd-description"> Alias for warning 27.

</dd></dl><p>The default setting is <span class="c003">-w +a-4-6-7-9-27-29-32..42-44-45-48-50-60</span>.
It is displayed by <span class="c003">ocamlc -help</span>.
Note that warnings 5 and 10 are not always triggered, depending on
the internals of the type checker.</p></dd><dt class="dt-description"><span class="c013"><span class="c003">-warn-error</span> <span class="c009">warning-list</span></span></dt><dd class="dd-description">
Mark as fatal the warnings specified in the argument <span class="c009">warning-list</span>.
The compiler will stop with an error when one of these warnings is
emitted. The <span class="c009">warning-list</span> has the same meaning as for
the <span class="c003">-w</span> option: a <span class="c003">+</span> sign (or an uppercase letter) marks the
corresponding warnings as fatal, a <span class="c003">-</span>
sign (or a lowercase letter) turns them back into non-fatal warnings,
and a <span class="c003">@</span> sign both enables and marks as fatal the corresponding
warnings.<p>Note: it is not recommended to use warning sets (i.e. letters) as
arguments to <span class="c003">-warn-error</span>
in production code, because this can break your build when future versions
of OCaml add some new warnings.</p><p>The default setting is <span class="c003">-warn-error -a+31</span> (only warning 31 is fatal).</p></dd><dt class="dt-description"><span class="c006">-warn-help</span></dt><dd class="dd-description">
Show the description of all available warning numbers.</dd><dt class="dt-description"><span class="c006">-where</span></dt><dd class="dd-description">
Print the location of the standard library, then exit.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-</span> <span class="c009">file</span></span></dt><dd class="dd-description">
Process <span class="c009">file</span> as a file name, even if it starts with a dash (<span class="c003">-</span>)
character.
</dd><dt class="dt-description"><span class="c013"><span class="c003">-help</span> or <span class="c003">--help</span></span></dt><dd class="dd-description">
Display a short usage summary and exit.</dd></dl>
<h5 class="paragraph" id="sec282">Contextual control of command-line options</h5>
<p>The compiler command line can be modified &#X201C;from the outside&#X201D;
with the following mechanisms. These are experimental
and subject to change. They should be used only for experimental and
development work, not in released packages.</p><dl class="description"><dt class="dt-description">
<span class="c006">OCAMLPARAM</span> (environment variable)</dt><dd class="dd-description">
Arguments that will be inserted before or after the arguments from the
command line.
</dd><dt class="dt-description"><span class="c006">ocaml_compiler_internal_params</span> (file in the stdlib directory)</dt><dd class="dd-description">
A mapping of file names to lists of arguments that
will be added to the command line (and <span class="c003">OCAMLPARAM</span>) arguments.
</dd><dt class="dt-description"><span class="c006">OCAML_FLEXLINK</span> (environment variable)</dt><dd class="dd-description">
Alternative executable to use on native
Windows for <span class="c003">flexlink</span> instead of the
configured value. Primarily used for bootstrapping.
</dd></dl>
<h2 class="section" id="sec283">9.3&#XA0;&#XA0;Modules and the file system</h2>
<p>This short section is intended to clarify the relationship between the
names of the modules corresponding to compilation units and the names
of the files that contain their compiled interface and compiled
implementation.</p><p>The compiler always derives the module name by taking the capitalized
base name of the source file (<span class="c003">.ml</span> or <span class="c003">.mli</span> file). That is, it
strips the leading directory name, if any, as well as the <span class="c003">.ml</span> or
<span class="c003">.mli</span> suffix; then, it set the first letter to uppercase, in order to
comply with the requirement that module names must be capitalized.
For instance, compiling the file <span class="c003">mylib/misc.ml</span> provides an
implementation for the module named <span class="c003">Misc</span>. Other compilation units
may refer to components defined in <span class="c003">mylib/misc.ml</span> under the names
<span class="c003">Misc.</span><span class="c009">name</span>; they can also do <span class="c003">open Misc</span>, then use unqualified
names <span class="c009">name</span>.</p><p>The <span class="c003">.cmi</span> and <span class="c003">.cmo</span> files produced by the compiler have the same
base name as the source file. Hence, the compiled files always have
their base name equal (modulo capitalization of the first letter) to
the name of the module they describe (for <span class="c003">.cmi</span> files) or implement
(for <span class="c003">.cmo</span> files).</p><p>When the compiler encounters a reference to a free module identifier
<span class="c003">Mod</span>, it looks in the search path for a file named <span class="c003">Mod.cmi</span> or <span class="c003">mod.cmi</span>
and loads the compiled interface
contained in that file. As a consequence, renaming <span class="c003">.cmi</span> files is not
advised: the name of a <span class="c003">.cmi</span> file must always correspond to the name
of the compilation unit it implements. It is admissible to move them
to another directory, if their base name is preserved, and the correct
<span class="c003">-I</span> options are given to the compiler. The compiler will flag an
error if it loads a <span class="c003">.cmi</span> file that has been renamed.</p><p>Compiled bytecode files (<span class="c003">.cmo</span> files), on the other hand, can be
freely renamed once created. That&#X2019;s because the linker never attempts
to find by itself the <span class="c003">.cmo</span> file that implements a module with a
given name: it relies instead on the user providing the list of <span class="c003">.cmo</span>
files by hand.</p>
<h2 class="section" id="sec284">9.4&#XA0;&#XA0;Common errors</h2>
<p> <a id="s:comp-errors"></a></p><p>This section describes and explains the most frequently encountered
error messages.</p><dl class="description"><dt class="dt-description"><span class="c013">Cannot find file <span class="c009">filename</span></span></dt><dd class="dd-description">
The named file could not be found in the current directory, nor in the
directories of the search path. The <span class="c009">filename</span> is either a
compiled interface file (<span class="c003">.cmi</span> file), or a compiled bytecode file
(<span class="c003">.cmo</span> file). If <span class="c009">filename</span> has the format <span class="c009">mod</span><span class="c003">.cmi</span>, this
means you are trying to compile a file that references identifiers
from module <span class="c009">mod</span>, but you have not yet compiled an interface for
module <span class="c009">mod</span>. Fix: compile <span class="c009">mod</span><span class="c003">.mli</span> or <span class="c009">mod</span><span class="c003">.ml</span>
first, to create the compiled interface <span class="c009">mod</span><span class="c003">.cmi</span>.<p>If <span class="c009">filename</span> has the format <span class="c009">mod</span><span class="c003">.cmo</span>, this
means you are trying to link a bytecode object file that does not
exist yet. Fix: compile <span class="c009">mod</span><span class="c003">.ml</span> first.</p><p>If your program spans several directories, this error can also appear
because you haven&#X2019;t specified the directories to look into. Fix: add
the correct <span class="c003">-I</span> options to the command line.</p></dd><dt class="dt-description"><span class="c013">Corrupted compiled interface <span class="c009">filename</span></span></dt><dd class="dd-description">
The compiler produces this error when it tries to read a compiled
interface file (<span class="c003">.cmi</span> file) that has the wrong structure. This means
something went wrong when this <span class="c003">.cmi</span> file was written: the disk was
full, the compiler was interrupted in the middle of the file creation,
and so on. This error can also appear if a <span class="c003">.cmi</span> file is modified after
its creation by the compiler. Fix: remove the corrupted <span class="c003">.cmi</span> file,
and rebuild it.</dd><dt class="dt-description"><span class="c013">This expression has type </span><span class="c009">t</span><sub>1</sub><span class="c013">, but is used with type </span><span class="c009">t</span><sub>2</sub></dt><dd class="dd-description">
This is by far the most common type error in programs. Type <span class="c009">t</span><sub>1</sub> is
the type inferred for the expression (the part of the program that is
displayed in the error message), by looking at the expression itself.
Type <span class="c009">t</span><sub>2</sub> is the type expected by the context of the expression; it
is deduced by looking at how the value of this expression is used in
the rest of the program. If the two types <span class="c009">t</span><sub>1</sub> and <span class="c009">t</span><sub>2</sub> are not
compatible, then the error above is produced.<p>In some cases, it is hard to understand why the two types <span class="c009">t</span><sub>1</sub> and
<span class="c009">t</span><sub>2</sub> are incompatible. For instance, the compiler can report that
&#X201C;expression of type <span class="c003">foo</span> cannot be used with type <span class="c003">foo</span>&#X201D;, and it
really seems that the two types <span class="c003">foo</span> are compatible. This is not
always true. Two type constructors can have the same name, but
actually represent different types. This can happen if a type
constructor is redefined. Example:
</p><pre>        type foo = A | B
        let f = function A -&gt; 0 | B -&gt; 1
        type foo = C | D
        f C
</pre><p>This result in the error message &#X201C;expression <span class="c003">C</span> of type <span class="c003">foo</span> cannot
be used with type <span class="c003">foo</span>&#X201D;.</p></dd><dt class="dt-description"><span class="c013">The type of this expression, <span class="c009">t</span>, contains type variables
that cannot be generalized</span></dt><dd class="dd-description">
Type variables (<span class="c003">'a</span>, <span class="c003">'b</span>, &#X2026;) in a type <span class="c009">t</span> can be in either
of two states: generalized (which means that the type <span class="c009">t</span> is valid
for all possible instantiations of the variables) and not generalized
(which means that the type <span class="c009">t</span> is valid only for one instantiation
of the variables). In a <span class="c003">let</span> binding <span class="c003">let </span><span class="c009">name</span><span class="c003"> = </span><span class="c009">expr</span>,
the type-checker normally generalizes as many type variables as
possible in the type of <span class="c009">expr</span>. However, this leads to unsoundness
(a well-typed program can crash) in conjunction with polymorphic
mutable data structures. To avoid this, generalization is performed at
<span class="c003">let</span> bindings only if the bound expression <span class="c009">expr</span> belongs to the
class of &#X201C;syntactic values&#X201D;, which includes constants, identifiers,
functions, tuples of syntactic values, etc. In all other cases (for
instance, <span class="c009">expr</span> is a function application), a polymorphic mutable
could have been created and generalization is therefore turned off for
all variables occurring in contravariant or non-variant branches of the
type. For instance, if the type of a non-value is <span class="c003">'a list</span> the
variable is generalizable (<span class="c003">list</span> is a covariant type constructor),
but not in <span class="c003">'a list -&gt; 'a list</span> (the left branch of <span class="c003">-&gt;</span> is
contravariant) or <span class="c003">'a ref</span> (<span class="c003">ref</span> is non-variant).<p>Non-generalized type variables in a type cause no difficulties inside
a given structure or compilation unit (the contents of a <span class="c003">.ml</span> file,
or an interactive session), but they cannot be allowed inside
signatures nor in compiled interfaces (<span class="c003">.cmi</span> file), because they
could be used inconsistently later. Therefore, the compiler
flags an error when a structure or compilation unit defines a value
<span class="c009">name</span> whose type contains non-generalized type variables. There
are two ways to fix this error:
</p><ul class="itemize"><li class="li-itemize">
Add a type constraint or a <span class="c003">.mli</span> file to give a monomorphic
type (without type variables) to <span class="c009">name</span>. For instance, instead of
writing
<pre>    let sort_int_list = Sort.list (&lt;)
    (* inferred type 'a list -&gt; 'a list, with 'a not generalized *)
</pre>write
<pre>    let sort_int_list = (Sort.list (&lt;) : int list -&gt; int list);;
</pre></li><li class="li-itemize">If you really need <span class="c009">name</span> to have a polymorphic type, turn
its defining expression into a function by adding an extra parameter.
For instance, instead of writing
<pre>    let map_length = List.map Array.length
    (* inferred type 'a array list -&gt; int list, with 'a not generalized *)
</pre>write
<pre>    let map_length lv = List.map Array.length lv
</pre></li></ul></dd><dt class="dt-description"><span class="c013">Reference to undefined global <span class="c009">mod</span></span></dt><dd class="dd-description">
This error appears when trying to link an incomplete or incorrectly
ordered set of files. Either you have forgotten to provide an
implementation for the compilation unit named <span class="c009">mod</span> on the command line
(typically, the file named <span class="c009">mod</span><span class="c003">.cmo</span>, or a library containing
that file). Fix: add the missing <span class="c003">.ml</span> or <span class="c003">.cmo</span> file to the command
line. Or, you have provided an implementation for the module named
<span class="c009">mod</span>, but it comes too late on the command line: the
implementation of <span class="c009">mod</span> must come before all bytecode object files
that reference <span class="c009">mod</span>. Fix: change the order of <span class="c003">.ml</span> and <span class="c003">.cmo</span>
files on the command line.<p>Of course, you will always encounter this error if you have mutually
recursive functions across modules. That is, function <span class="c003">Mod1.f</span> calls
function <span class="c003">Mod2.g</span>, and function <span class="c003">Mod2.g</span> calls function <span class="c003">Mod1.f</span>.
In this case, no matter what permutations you perform on the command
line, the program will be rejected at link-time. Fixes:
</p><ul class="itemize"><li class="li-itemize">
Put <span class="c003">f</span> and <span class="c003">g</span> in the same module.
</li><li class="li-itemize">Parameterize one function by the other.
That is, instead of having
<pre>mod1.ml:    let f x = ... Mod2.g ...
mod2.ml:    let g y = ... Mod1.f ...
</pre>define
<pre>mod1.ml:    let f g x = ... g ...
mod2.ml:    let rec g y = ... Mod1.f g ...
</pre>and link <span class="c003">mod1.cmo</span> before <span class="c003">mod2.cmo</span>.
</li><li class="li-itemize">Use a reference to hold one of the two functions, as in :
<pre>mod1.ml:    let forward_g =
                ref((fun x -&gt; failwith "forward_g") : &lt;type&gt;)
            let f x = ... !forward_g ...
mod2.ml:    let g y = ... Mod1.f ...
            let _ = Mod1.forward_g := g
</pre></li></ul></dd><dt class="dt-description"><span class="c013">The external function <span class="c009">f</span> is not available</span></dt><dd class="dd-description">
This error appears when trying to link code that calls external
functions written in C. As explained in
chapter&#XA0;<a href="intfc.html#c%3Aintf-c">20</a>, such code must be linked with C libraries that
implement the required <span class="c009">f</span> C function. If the C libraries in
question are not shared libraries (DLLs), the code must be linked in
&#X201C;custom runtime&#X201D; mode. Fix: add the required C libraries to the
command line, and possibly the <span class="c003">-custom</span> option.</dd></dl>
<h2 class="section" id="sec285">9.5&#XA0;&#XA0;Warning reference</h2>
<p> <a id="s:comp-warnings"></a></p><p>This section describes and explains in detail some warnings:</p>
<h3 class="subsection" id="sec286">9.5.1&#XA0;&#XA0;Warning 9: missing fields in a record pattern</h3>
<p>When pattern matching on records, it can be useful to match only few
fields of a record. Eliding fields can be done either implicitly
or explicitly by ending the record pattern with <span class="c003">; _</span>.
However, implicit field elision is at odd with pattern matching
exhaustiveness checks.
Enabling warning 9 prioritizes exhaustiveness checks over the
convenience of implicit field elision and will warn on implicit
field elision in record patterns. In particular, this warning can
help to spot exhaustive record pattern that may need to be updated
after the addition of new fields to a record type.</p><pre>type 'a point = {x='a ;y='a}
let dx { x } = x (* implicit field elision: trigger warning 9 *)
let dy { y; _ } = y (* explicit field elision: do not trigger warning 9 *)
</pre>
<h3 class="subsection" id="sec287">9.5.2&#XA0;&#XA0;Warning 52: fragile constant pattern</h3>
<p>
<a id="ss:warn52"></a></p><p>Some constructors, such as the exception constructors <span class="c003">Failure</span> and
<span class="c003">Invalid_argument</span>, take as parameter a <span class="c003">string</span> value holding
a text message intended for the user.</p><p>These text messages are usually not stable over time: call sites
building these constructors may refine the message in a future
version to make it more explicit, etc. Therefore, it is dangerous to
match over the precise value of the message. For example, until
OCaml 4.02, <span class="c003">Array.iter2</span> would raise the exception
</p><pre>  Invalid_argument "arrays must have the same length"
</pre><p> Since 4.03 it raises the more helpful message
</p><pre>  Invalid_argument "Array.iter2: arrays must have the same length"
</pre><p> but this means that any code of the form
</p><pre>  try ...
  with Invalid_argument "arrays must have the same length" -&gt; ...
</pre><p> is now broken and may suffer from uncaught exceptions.</p><p>Warning 52 is there to prevent users from writing such fragile code
in the first place. It does not occur on every matching on a literal
string, but only in the case in which library authors expressed
their intent to possibly change the constructor parameter value in
the future, by using the attribute <span class="c003">ocaml.warn_on_literal_pattern</span>
(see the manual section on builtin attributes in
<a href="extn.html#ss%3Abuiltin-attributes">8.16.1</a>):
</p><pre>  type t =
    | Foo of string [@ocaml.warn_on_literal_pattern]
    | Bar of string

  let no_warning = function
    | Bar "specific value" -&gt; 0
    | _ -&gt; 1

  let warning = function
    | Foo "specific value" -&gt; 0
    | _ -&gt; 1

&gt;    | Foo "specific value" -&gt; 0
&gt;          ^^^^^^^^^^^^^^^^
&gt; Warning 52: Code should not depend on the actual values of
&gt; this constructor's arguments. They are only for information
&gt; and may change in future versions. (See manual section 8.5)
</pre><p>
In particular, all built-in exceptions with a string argument have
this attribute set: <span class="c003">Invalid_argument</span>, <span class="c003">Failure</span>, <span class="c003">Sys_error</span> will
all raise this warning if you match for a specific string argument.</p><p>If your code raises this warning, you should <em>not</em> change the
way you test for the specific string to avoid the warning (for
example using a string equality inside the right-hand-side instead
of a literal pattern), as your code would remain fragile. You should
instead enlarge the scope of the pattern by matching on all possible
values.</p><pre>
let warning = function
  | Foo _ -&gt; 0
  | _ -&gt; 1
</pre><p>
This may require some care: if the scrutinee may return several
different cases of the same pattern, or raise distinct instances of
the same exception, you may need to modify your code to separate
those several cases.</p><p>For example,
</p><pre>try (int_of_string count_str, bool_of_string choice_str) with
  | Failure "int_of_string" -&gt; (0, true)
  | Failure "bool_of_string" -&gt; (-1, false)
</pre><p> should be rewritten into more atomic tests. For example,
using the <span class="c003">exception</span> patterns documented in Section&#XA0;<a href="extn.html#s%3Aexception-match">8.19</a>,
one can write:
</p><pre>match int_of_string count_str with
  | exception (Failure _) -&gt; (0, true)
  | count -&gt;
    begin match bool_of_string choice_str with
    | exception (Failure _) -&gt; (-1, false)
    | choice -&gt; (count, choice)
    end
</pre><p>
The only case where that transformation is not possible is if a given
function call may raise distinct exceptions with the same constructor
but different string values. In this case, you will have to check for
specific string values. This is dangerous API design and it should be
discouraged: it&#X2019;s better to define more precise exception constructors
than store useful information in strings.</p>
<h3 class="subsection" id="sec288">9.5.3&#XA0;&#XA0;Warning 57: Ambiguous or-pattern variables under guard</h3>
<p>
<a id="ss:warn57"></a></p><p>The semantics of or-patterns in OCaml is specified with
a left-to-right bias: a value <span class="c009">v</span> matches the pattern <span class="c009">p</span> <span class="c003">|</span> <span class="c009">q</span>
if it matches <span class="c009">p</span> or <span class="c009">q</span>, but if it matches both,
the environment captured by the match is the environment captured by
<span class="c009">p</span>, never the one captured by <span class="c009">q</span>.</p><p>While this property is generally intuitive, there is at least one specific
case where a different semantics might be expected.
Consider a pattern followed by a when-guard:
<span class="c003">|</span>&#XA0;<span class="c009">p</span>&#XA0;<span class="c003">when</span>&#XA0;<span class="c009">g</span>&#XA0;<span class="c003">-&gt;</span>&#XA0;<span class="c009">e</span>, for example:
</p><pre>     | ((Const x, _) | (_, Const x)) when is_neutral x -&gt; branch
</pre><p> The semantics is clear:
match the scrutinee against the pattern, if it matches, test the guard,
and if the guard passes, take the branch.
In particular, consider the input <span class="c003">(Const</span>&#XA0;<span class="c009">a</span><span class="c003">, Const</span>&#XA0;<span class="c009">b</span><span class="c003">)</span>, where
<span class="c009">a</span> fails the test <span class="c003">is_neutral</span>&#XA0;<span class="c009">a</span>, while <span class="c009">b</span> passes the test
<span class="c003">is_neutral</span>&#XA0;<span class="c009">b</span>. With the left-to-right semantics, the clause above is
<em>not</em> taken by its input: matching <span class="c003">(Const</span>&#XA0;<span class="c009">a</span><span class="c003">, Const</span>&#XA0;<span class="c009">b</span><span class="c003">)</span>
against the or-pattern succeeds in the left branch, it returns the
environment <span class="c009">x</span>&#XA0;<span class="c003">-&gt;</span>&#XA0;<span class="c009">a</span>, and then the guard
<span class="c003">is_neutral</span>&#XA0;<span class="c009">a</span> is tested and fails, the branch is not taken.</p><p>However, another semantics may be considered more natural here:
any pair that has one side passing the test will take the branch. With this
semantics the previous code fragment would be equivalent to
</p><pre>     | (Const x, _) when is_neutral x -&gt; branch
     | (_, Const x) when is_neutral x -&gt; branch
</pre><p> This is <em>not</em> the semantics adopted by OCaml.</p><p>Warning 57 is dedicated to these confusing cases where the
specified left-to-right semantics is not equivalent to a non-deterministic
semantics (any branch can be taken) relatively to a specific guard.
More precisely, it warns when guard uses &#X201C;ambiguous&#X201D; variables, that are bound
to different parts of the scrutinees by different sides of a or-pattern.

</p>
<hr>
<a href="extn.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="toplevel.html"><img src="next_motif.gif" alt="Next"></a>
</body>
</html>
